package com.kkd.kkdapp.pinnedsection;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.DealDetailItemBean;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView.PinnedSectionListAdapter;

/**
 * 分好类的适配器.加载的是分好组的list数据
 * 
 * @author max.chengdu 2015年11月29日
 * 
 */
public class WarnDetailAdapter extends BaseAdapter implements
		PinnedSectionListAdapter {
	// 时间以及相关信息
	private ArrayList<DealDetailItemBean> list;
	private Context mContext;
	private LayoutInflater inflater;
	private DecimalFormat df = new DecimalFormat("#,###.00");

	public ArrayList<DealDetailItemBean> getList() {
		return list;
	}

	public void setList(ArrayList<DealDetailItemBean> list) {
		if (list != null) {
			this.list = list;
		} else {
			list = new ArrayList<DealDetailItemBean>();
		}
	}

	public WarnDetailAdapter(ArrayList<DealDetailItemBean> list,
			Context mContext) {
		super();
		this.setList(list);
		this.mContext = mContext;
		inflater = LayoutInflater.from(mContext);
	}

	final static class ViewHolder {
		TextView item_date, item_content;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public DealDetailItemBean getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DealDetailItemBean bean = getItem(position);
		ViewHolder viewHolder = null;
		ViewHolderPinner viewHolderPinner = null;
		ViewHolderContent viewHolderContent = null;
		if (convertView == null) {
			if (bean.type == DealDetailItemBean.SECTION) {// 悬停的标题
				viewHolderPinner = new ViewHolderPinner();
				convertView = inflater.inflate(R.layout.item_pinner_listview,
						parent, false);
				viewHolderPinner.tv_title_pinner = (TextView) convertView
						.findViewById(R.id.tv_title_pinner);
				convertView.setTag(viewHolderPinner);
			} else {
				viewHolderContent = new ViewHolderContent();
				convertView = inflater.inflate(
						R.layout.item_content_pinner_listview, parent, false);
				viewHolderContent.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				viewHolderContent.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				viewHolderContent.tv_num = (TextView) convertView
						.findViewById(R.id.tv_num);
				viewHolderContent.tv_flag = (TextView) convertView
						.findViewById(R.id.tv_flag);
				viewHolderContent.line = convertView.findViewById(R.id.line);
				viewHolderContent.bottom = convertView
						.findViewById(R.id.bottom);
				viewHolderContent.creditor_number_tv = (TextView) convertView
						.findViewById(R.id.creditor_number_tv);
				convertView.setTag(viewHolderContent);

				//
				// viewHolder = new ViewHolder();
				// convertView = inflater.inflate(R.layout.item_list_layout,
				// parent, false);
				// viewHolder.item_date = (TextView) convertView
				// .findViewById(R.id.item_date);
				// viewHolder.item_content = (TextView) convertView
				// .findViewById(R.id.item_content);
				// convertView.setTag(viewHolder);
			}
		} else {
			if (bean.type == DealDetailItemBean.SECTION) {// 悬停的标题
				viewHolderPinner = (ViewHolderPinner) convertView.getTag();
			} else {
				viewHolderContent = (ViewHolderContent) convertView.getTag();
			}
		}
		String createTime = list.get(position).getDealDetailItem()
				.getCreateTime();
		// 当item属于标题的时候,就只显示分类的日期yyyy-MM-dd
		if (bean.type == DealDetailItemBean.SECTION) {
			viewHolderPinner.tv_title_pinner.setText(createTime.substring(0, 4)
					+ "年" + createTime.substring(5) + "月");
		}
		// 当item属于内容的时候,就只显示分类的日期HH:mm:ss,和其他类容
		else {
			// convertView.setBackgroundColor(Color.parseColor("#aa00aa00"));
			// viewHolder.item_date.setText(list.get(position).getDealDetailItem()
			// .getCreateTime());
			// viewHolder.item_content.setText(list.get(position)
			// .getDealDetailItem().getSummary());
			viewHolderContent.tv_title.setSelected(true);
			if ("3018".equals(list.get(position).getDealDetailItem()
					.getTradeType())
					&& !TextUtils.isEmpty(list.get(position)
							.getDealDetailItem().getExt())) {// 说明是邀请好友返利
				viewHolderContent.tv_title.setText(list.get(position)
						.getDealDetailItem().getExt());
			} else {
				viewHolderContent.tv_title.setText(list.get(position)
						.getDealDetailItem().getSummary());
			}
			String timeAndExtra = list.get(position).getDealDetailItem()
					.getCreateTime().replaceAll("-", "/");
			viewHolderContent.tv_time.setText(timeAndExtra);
			String amountString = df.format(list.get(position)
					.getDealDetailItem().getAmount());
			String value = amountString.startsWith(".") ? ("0" + amountString)
					: amountString;
			if (list.get(position).getDealDetailItem().getType() == 0) {
				viewHolderContent.tv_num.setText("-" + value);
				viewHolderContent.tv_num.setTextColor(Color
						.parseColor("#75d441"));
			} else {
				viewHolderContent.tv_num.setText("+" + value);
				viewHolderContent.tv_num.setTextColor(Color
						.parseColor("#ff7301"));
			}
			int status = list.get(position).getDealDetailItem().getStatus();
			String statusName = "";
			if ("3004".equals(list.get(position).getDealDetailItem()
					.getTradeType())
					|| "3005".equals(list.get(position).getDealDetailItem()
							.getTradeType())) {
				if (status == 0) {
					statusName = "未入账";
				} else if (status == 1) {
					statusName = "已入账";
				} else if (status == 2) {
					statusName = "入账失败";
				}
				// statusName = (status == 1 ? "已入账" : "未入账");
			} else {
				statusName = "已入账";
			}

			if ("未入账".equals(statusName)) {
				viewHolderContent.tv_flag.setText("未入账");
				viewHolderContent.tv_flag.setVisibility(View.VISIBLE);
			} else if ("入账失败".equals(statusName)) {
				viewHolderContent.tv_flag.setText("入账失败");
				viewHolderContent.tv_flag.setVisibility(View.VISIBLE);
			} else {
				viewHolderContent.tv_flag.setVisibility(View.GONE);
			}

			// 债权份数处理
			String parts = list.get(position).getDealDetailItem().getParts();
			if (TextUtils.isEmpty(parts)) {
				viewHolderContent.creditor_number_tv.setVisibility(View.GONE);
			} else {
				viewHolderContent.tv_flag.setVisibility(View.GONE);
				viewHolderContent.creditor_number_tv
						.setVisibility(View.VISIBLE);
				viewHolderContent.creditor_number_tv.setText(parts);
			}

			if (position == list.size() - 1
					|| getItem(position + 1).type == DealDetailItemBean.SECTION) {
				viewHolderContent.line.setVisibility(View.GONE);
				// if (position == list.size() - 1) {
				// viewHolderContent.bottom.setVisibility(View.GONE);
				// } else {
				// viewHolderContent.bottom.setVisibility(View.VISIBLE);
				// }
			} else {
				viewHolderContent.line.setVisibility(View.VISIBLE);
				// viewHolderContent.bottom.setVisibility(View.GONE);
			}
		}
		return convertView;

	}

	// 判断是否是属于标题悬浮的
	@Override
	public boolean isItemViewTypePinned(int viewType) {
		return viewType == DealDetailItemBean.SECTION;
	}

	// arraylist的数据里面有2种类型,标题和内容
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return ((DealDetailItemBean) getItem(position)).type;
	}

	class ViewHolderPinner {
		TextView tv_title_pinner;
	}

	class ViewHolderContent {
		TextView tv_title;
		TextView tv_time;
		TextView tv_num;
		TextView tv_flag;
		TextView creditor_number_tv;
		View line;
		View bottom;
	}
}
